class Solution {
public:
    bool isPalindrome(ListNode* head) {
        if (head == nullptr || head->next == nullptr)
            return true;
        ListNode* a = head;
        ListNode* b = head->next;
        ListNode* headlist = nullptr;
        while (b != nullptr && b->next != nullptr)
        {
            ListNode* t = a->next;
            a->next = headlist;
            headlist = a;
            a = t;
            b = b->next->next;
        }
        if (b != nullptr)
        {
            b = a->next;
            a->next = headlist;
            headlist = a;
            a = b;
        }
        else
            a = a->next;
        while (a != nullptr && headlist != nullptr)
        {
            if (a->val != headlist->val)
                return false;
            a = a->next;
            headlist = headlist->next;
        }
        return true;
    }
};